package com.juntu.loginlib.ui;

import android.text.TextUtils;
import android.widget.Toast;

import com.juntu.loginlib.LoginCenter;
import com.juntu.loginlib.contact.AppConfigContact;
import com.juntu.loginlib.contact.LoginConstant;
import com.juntu.loginlib.contact.LoginResultContact;
import com.juntu.loginlib.entity.LoginInfo;
import com.juntu.loginlib.ex.LoginException;
import com.juntu.loginlib.util.LoginUtil;

/**
 * Created by cj on 2021/2/19.
 * Email:codesexy@163.com
 * Function:
 * desc:
 */
public abstract class BaseSplashActivity extends BaseActivity {


    /**
     * 打开登录页面
     */
    protected abstract void startLoginActivity();


    /**
     * 设置获取配置信息的
     *
     * @return
     */
    protected abstract String getSystemConfigType();


    @Override
    public void loadData() {
        //获取配置信息
        String configType = getSystemConfigType();
        showDialog("初始化中...");

        //获取配置信息
        LoginCenter.getInstance().getAppConfig(configType, false, new AppConfigContact() {
            @Override
            public void getConfigSuccess(String config) {
                //存储到本地
                LoginUtil.saveSp(LoginConstant.KEY_CONFIG, config);
                parseConfig(config);
                checkLoginResult();
            }

            @Override
            public void getConfigError(LoginException exception) {
                dismissDialog();
                showErrorTips("获取配置信息失败了");
                Toast.makeText(BaseSplashActivity.this, exception.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });


    }


    private void checkLoginResult() {

        //是否已经记住密码
        Boolean isRemember = LoginUtil.getBoolean(LoginConstant.REMEMBER_PSW);
        //获取存储在本地的场所id
        String placeId = LoginUtil.getStr(LoginConstant.PLACE_ID);
        //获取存储在本地的设备类型
        String deviceType = LoginUtil.getStr(LoginConstant.DEVICE_TYPE);
        //本地的用户名
        String userName = LoginUtil.getStr(LoginConstant.USER_NAME);
        //本地的密码
        String userPsw = LoginUtil.getStr(LoginConstant.USER_PSW);

        if (isRemember && !TextUtils.isEmpty(placeId)
                && !TextUtils.isEmpty(deviceType)
                && !TextUtils.isEmpty(userName)
                && !TextUtils.isEmpty(userPsw)) {
            //开始登录成功之后需要打开主界面
            LoginInfo info = new LoginInfo(userName, userPsw, deviceType, placeId);
            LoginCenter.getInstance().doLogin(info, new LoginResultContact() {
                @Override
                public void onSuccess(String info) {
                    saveLoginInfo(info);
                    dismissDialog();
                    startMainActivity();
                }

                @Override
                public void onError(LoginException ex) {
                    dismissDialog();
                    showErrorTips("登录失败了");
                    Toast.makeText(BaseSplashActivity.this, ex.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            dismissDialog();
            //打开登录界面
            startLoginActivity();
        }

    }


}
